<template>
  <div class="app-container">
    <h3>修改用户名</h3>
    <el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" label-width="100px" class="ruleForm">
      <el-form-item label="原用户名:">
        {{name}}
      </el-form-item>
      <el-form-item label="新用户名:" prop="name">
        <el-input type="text" v-model="ruleForm2.name" 
        placeholder="请输入新的用户名"
        auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm2')">提交</el-button>
        <el-button @click="resetForm('ruleForm2')">重置</el-button>
      </el-form-item>
    </el-form>   
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  export default {
    computed:{
      ...mapGetters([
        'name'
      ])
    }
    ,
    data() {
      var validateName = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入新用户名'));
        } else {
          if (this.ruleForm2.name !== '') {
            this.$refs.ruleForm2.validateField('name');
          }
          callback();
        }
      };
      return {
        ruleForm2: {
          name: ''
        },
        rules2: {
          checkName: [
            { validator: validateName, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>